package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class RoTRiftAct4Pane extends GDAbstractRiftPane {
    private JCheckBox cbPandemoniumFortress;
    private JCheckBox cbPlainsDispair;
    private JCheckBox cbCityDamned;
    private JCheckBox cbRiverFlame;
    private int difficulty;
    private boolean changed;

    public RoTRiftAct4Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbPandemoniumFortress).addComponent(this.cbPlainsDispair).addComponent(this.cbCityDamned).addComponent(this.cbRiverFlame));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbPandemoniumFortress)).addGroup(layout.createParallelGroup().addComponent(this.cbPlainsDispair)).addGroup(layout.createParallelGroup().addComponent(this.cbCityDamned)).addGroup(layout.createParallelGroup().addComponent(this.cbRiverFlame));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbPandemoniumFortress, this.cbPlainsDispair);
        layout.linkSize(0, this.cbPandemoniumFortress, this.cbCityDamned);
        layout.linkSize(0, this.cbPandemoniumFortress, this.cbRiverFlame);
        layout.linkSize(1, this.cbPandemoniumFortress, this.cbPlainsDispair);
        layout.linkSize(1, this.cbPandemoniumFortress, this.cbCityDamned);
        layout.linkSize(1, this.cbPandemoniumFortress, this.cbRiverFlame);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT4"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbPandemoniumFortress == null) {
            (this.cbPandemoniumFortress = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbPandemoniumFortress.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_04_PANDEMONIUM_FORTRESS"));
        this.cbPandemoniumFortress.setFont(fntCheck);
        if (this.cbPlainsDispair == null) {
            (this.cbPlainsDispair = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbPlainsDispair.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_04_PLAINS_DESPAIR"));
        this.cbPlainsDispair.setFont(fntCheck);
        if (this.cbCityDamned == null) {
            (this.cbCityDamned = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCityDamned.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_04_CITY_DAMNED"));
        this.cbCityDamned.setFont(fntCheck);
        if (this.cbRiverFlame == null) {
            (this.cbRiverFlame = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbRiverFlame.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_04_RIVER_FLAME"));
        this.cbRiverFlame.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbPandemoniumFortress.setSelected(selected);
        this.cbPlainsDispair.setSelected(selected);
        this.cbCityDamned.setSelected(selected);
        this.cbRiverFlame.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbPandemoniumFortress.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_04_PANDEMONIUM_FORTRESS);
        }
        if (this.cbPlainsDispair.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_04_PLAINS_DESPAIR);
        }
        if (this.cbCityDamned.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_04_CITY_DAMNED);
        }
        if (this.cbRiverFlame.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_04_RIVER_FLAME);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_04_PANDEMONIUM_FORTRESS)) {
                this.cbPandemoniumFortress.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_04_PLAINS_DESPAIR)) {
                this.cbPlainsDispair.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_04_CITY_DAMNED)) {
                this.cbCityDamned.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_04_RIVER_FLAME)) {
                this.cbRiverFlame.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            RoTRiftAct4Pane.this.changed = true;
        }
    }
}
